package org.zxw.controller.userSystem;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zxw.pojo.dto.LoginRequest;
import org.zxw.pojo.vo.Result;
import org.zxw.service.LoginAndRegisterService;

@RestController
@RequestMapping("/api/v1/user")
@Slf4j
public class LoginAndRegisterController {

    private final LoginAndRegisterService loginAndRegisterService;

    public LoginAndRegisterController(LoginAndRegisterService loginAndRegisterService) {
        this.loginAndRegisterService = loginAndRegisterService;
    }

    /**
     * 用户登录
     *
     * @param loginRequest
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest loginRequest) {
        log.info("用户登录");
        String jwt = loginAndRegisterService.login(loginRequest.getUsername(), loginRequest.getPassword());
        if(jwt == null){
            log.info("用户名或密码错误");
            return Result.error("用户名或密码错误");
        }
        System.out.println("jwt:"+jwt);
        log.info("登录成功");
        return Result.success(jwt);
    }
}